//
//  BIZHotTopicDetailVC.m
//  biz
//
//  Created by zhangfuwei on 2017/7/8.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZHotTopicDetailVC.h"
//#import <WebKit/WebKit.h>
#import "BIZHotTopicChildrenListModel.h"

@interface BIZHotTopicDetailVC ()
/** title */
@property (nonatomic, strong) UILabel *titleLabel;
/** time */
@property (nonatomic, strong) UIButton *timeButton;


@end

@implementation BIZHotTopicDetailVC{
    UIWebView *_webView;
    NSString    *_uuid;
}


-(instancetype)initWithUUID:(NSString *)uuid {
    self = [super init];
    if (self) {
        _uuid = uuid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"热点问题详情";
    [self initSubviews];
    [self requestDetailContent];
}

- (void)initSubviews {
    
    self.timeButton.layer.borderColor = [UIColor zfw_ColorWithHex:0xeeedee alpha:1.0].CGColor;
    self.timeButton.layer.borderWidth = 1.0f;
    _webView = [[UIWebView alloc] init];

    [self.view addSubview:self.titleLabel];
    [self.view insertSubview:self.timeButton belowSubview:self.titleLabel];
    [self.view addSubview:_webView];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(5);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeButton.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

#pragma mark -- request

- (void)requestDetailContent {
    
    [RequestTool POST:kHotTopicIssueeDetailURL
           parameters:@{@"uuid":_uuid}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"热点问题(详情)--%@",responseObject);
                  NSString *code = responseObject[@"code"];

                  if (code.intValue == 1) {
                      NSNumber *time = responseObject[@"data"][@"createDate"];
                      NSString *title = responseObject[@"data"][@"topic"];
                      NSString *content = responseObject[@"data"][@"content"];
                      NSString *HTML = [content stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];

                      self.titleLabel.text = title;
                      [self.timeButton setTitle:[NSDate stringFromTimestamp:time.longLongValue] forState:UIControlStateNormal];
                      [_webView loadHTMLString:HTML baseURL:nil];
                      
                  }else if (code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:responseObject[@"des"]];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}

#pragma mark -- Lazyload

-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor zfw_ColorWithHex:0x333333 alpha:1.0];
        _titleLabel.font = [UIFont fontWithName:CHINESE_BOLD_FONT_NAME size:15];
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}


- (UIButton *)timeButton {
    if (!_timeButton) {
        _timeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 150-37, self.view.bounds.size.width, 37)];
        _timeButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _timeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        _timeButton.titleLabel.font = CHINESE_SYSTEM(10);
        [_timeButton setTitleColor:[UIColor zfw_ColorWithHex:0x999999 alpha:1.0] forState:UIControlStateNormal];
    }
    return _timeButton;
}

@end
